<%@ page import="com.cinsea.security.service.RightService" %>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/common/common.jsp"%>


<%
int pageSize=20;
    String dowid=StringUtil.null2String(request.getParameter("directoryid"));
    String types=StringUtil.null2String(request.getParameter("types"));
    String mytopics=StringUtil.null2String(request.getParameter("mytopics"));
    RightService rightService=(RightService)Env.getBean("rightService");
    boolean isauth =false;
    if(!StringUtil.isEmpty(dowid))
           isauth= rightService.checkOpttype(dowid, 2);
String cmstr="{header:'主题',dataIndex:'topic',width:200,sortable:true,renderer:renderTopic},{header:'回复',dataIndex:'replycount',width:30,sortable:false},{header:'最后回复时间',dataIndex:'replytime',width:65,sortable:false,renderer:renderReply}";
String    fieldstr="'processid','isnew','creator','topic','createtime','replycount','replyuser','replytime'";
    String action=request.getContextPath()+ "/ext/com.cinsea.topic.ext.TopicAction?action=gettopics&types="+types;
    if(StringUtil.isEmpty(types)) action=request.getContextPath()+ "/topic.do?action=gettopics";
    if(mytopics.equals("1")) action=request.getContextPath()+"/topic.do?action=getmytopics";

%>
<html>
  <head>
  <%@ include file="/common/head.jsp"%>
    <style>
           .unread {
        font-weight:bold;
    }
   </style>
  <script type="text/javascript" src="${ctx}/js/ext/ux/storemenu.js"></script>
  <script type="text/javascript" src="${ctx}/js/ext/ux/AutoRefresher.js"></script>
  <script type="text/javascript">

   Ext.onReady(function(){
       Ext.QuickTips.init();
       var topictpl = new Ext.XTemplate(
            '<p>{creator} 发表于  {createtime}</p>',
            '{topic}'
        );
       function renderTopic(value, p, record) {
           p.attr = 'style="white-space:normal;word-break:break-all;line-height: 1.4"';
           value= topictpl.apply(record.data);
           if(record.get('isnew')&&record.get('isnew')=="1"){
               value=String.format('<span class=unread>{0}</span>',value);
            }
           return value;
           }
       function renderReply(value, p, record) {
                 p.attr = 'style="white-space:normal;word-break:break-all;line-height: 1.4"';
                 return value;
                 }
    var sm=new Ext.grid.RowSelectionModel();
       var cm = new Ext.grid.ColumnModel([<%=cmstr%>]);
    // create the Data Store
    var store = new Ext.data.JsonStore({
        url:'<%=action%>',
        root: 'result',
        totalProperty: 'totalCount',
        fields: [<%=fieldstr%>],
        baseParams:{dowid:'<%=dowid%>',sort:'modifytime',dir:'desc'},
        remoteSort: true
    });

    // create the editor grid
    var autorefresh=new Ext.ux.grid.AutoRefresher();
    var grid = new Ext.grid.GridPanel({
        region: 'west',
        store: store,
        cm: cm,
        trackMouseOver:false,
        sm:sm ,
        loadMask: true,
        split:true,
        collapsible:true,
        collapseMode:'mini',
        width:400,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            sortAscText:'升序',
            sortDescText:'降序',
            columnsText:'列定义',
            getRowClass : function(record, rowIndex, p, store) {
                return 'unread';//'x-grid3-row-collapsed';
            }
        },
        <%if(isauth){%>
        tbar: [{
            text: '新建主题',
            iconCls:Ext.ux.iconMgr.getIcon('add'),
            handler : function() {
                url='${ctx}/topic/topiccreate.jsp?directoryid=<%=dowid%>&types=<%=types%>';
                rightPanel.getComponent('topic').setSrc(url);
            }
        }],
        <%}%>
        bbar: new Ext.PagingToolbar({
            pageSize: <%=pageSize%>,
                            plugins: autorefresh,
            store: store,
            displayInfo: false,
            beforePageText:"第",
            afterPageText:"页/{0}",
            firstText:"第一页",
            prevText:"上页",
            nextText:"下页",
            lastText:"最后页",
            displayMsg: '显示 {0} - {1}条记录 / {2}',
            emptyMsg: "没有结果返回"
        })
    });
    store.on('beforeload',function(){
        autorefresh.start();
    });
    var rightPanel = new Ext.Panel({
        region:'center',
        autoScroll:true,
        layout:'border',
        items:[{
            //title: "subject",
            id:'topic',
            xtype     :'iframepanel',
            region:'center',
            frameConfig: {
                autoCreate:{ id:'topic', name:'topic', frameborder:0 },
                eventsFollowFrameLinks : false
            },
            closable:false,
            autoScroll:true
        }]
    })


    sm.on('rowselect', function(selMdl, rowIndex, rec) {
        var processid = rec.get("processid");
        var row=grid.view.getRow(rowIndex);
        var innerhtml=row.innerHTML;
        innerhtml=innerhtml.replace(/unread/gm,'');
        row.innerHTML=innerhtml;
        var url = '${ctx}/topic/topicview.jsp?processid=' + processid;

        rightPanel.getComponent('topic').setSrc(url);
    });
       grid.on('rowclick', function (selMdl, rowIndex, e) {
           rightPanel.getComponent('topic').getFrameWindow().location.reload(true);
       });


       //Viewport
	var viewport = new Ext.Viewport({
        layout: 'border',
        items: [grid,rightPanel]
	});
    store.load({params:{start:0, limit:<%=pageSize%>}});



   })
   </script>
  </head>
  <body >
  </body>
</html>
